package com.evil.DAO;

import com.evil.DB_Utils.DB_Utils;

import java.util.ArrayList;

/********************************************
 * 工程名：Eda
 * 包名：com.evil.DAO
 * 时间：16-9-7 下午12:28
 * 作者：白雨濃 evil
 * 邮箱：evilbai@foxmail.com
 * 内容：公告推送数据库操作
 ********************************************/
public class SendMessageDAO
{
    /**
     * 查找公共公告
     *
     * @return ArrayList<String> index 0->id,index 1->msg.
     */
    public ArrayList<String> find_public_msg()
    {
        String url = "SELECT\n" +
                "sendmessage_public.id,\n" +
                "sendmessage_public.msg\n" +
                "FROM\n" +
                "sendmessage_public";

        ArrayList<String> list = new ArrayList<>();

        DB_Utils.query(url).build().get(next ->
        {
            if (next.next())
            {
                list.add(next.getString(1));
                list.add(next.getString(2));
            }
        }).close();

        return list;
    }


    /**
     * 查找私人公告
     *
     * @param phone 来源手机号
     * @return ArrayList<String> index 0->phone,index 1->msg,index 2->datetime(String).
     */
    public ArrayList<String> find_private_msg(String phone)
    {
        String url = "SELECT\n" +
                "sendmessage_private.phone,\n" +
                "sendmessage_private.msg,\n" +
                "sendmessage_private.datetime\n" +
                "FROM\n" +
                "sendmessage_private\n" +
                "WHERE\n" +
                "sendmessage_private.phone = ?";

        ArrayList<String> list = new ArrayList<>();

        DB_Utils.query(url).add(phone).build().get(next ->
        {
            if (next.next())
            {
                list.add(next.getString(1));
                list.add(next.getString(2));
                list.add(next.getDateTime(3));
            }
        }).close();

        return list;
    }

    /**
     * 读取完成后，即删除此条信息
     *
     * @param phone
     * @return
     */
    public boolean delete_private_msg(String phone)
    {
        String re = DB_Utils.delete().bulid("sendmessage_private", "phone", phone).close();
        return re.equals("1");
    }


}
